package com.hyperboost.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyperboost.constant.MessageConstant;
import com.hyperboost.dto.ShareCommentDTO;
import com.hyperboost.dto.ShareCommentPageQueryDTO;
import com.hyperboost.dto.ShareCommentReplyQueryDTO;
import com.hyperboost.dto.SharePageQueryDto;
import com.hyperboost.entity.ShareComment;
import com.hyperboost.exception.NoPermissionToDelete;
import com.hyperboost.mapper.ShareCommentMapper;
import com.hyperboost.mapper.ShareMapper;
import com.hyperboost.result.PageResult;
import com.hyperboost.service.ShareCommentService;
import com.hyperboost.utils.ThreadLocalUtil;
import com.hyperboost.vo.ShareCommentReplyVO;
import com.hyperboost.vo.ShareCommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ShareCommentServiceImpl implements ShareCommentService {

    @Autowired
    private ShareCommentMapper commentMapper;

    @Autowired
    private ShareMapper shareMapper;

    /**
     * 新增评论
     *
     * @param commentDTO
     */
    @Override
    public void addComment(ShareCommentDTO commentDTO) {

        // new一个ShareComment对象，作为评论数据存储
        ShareComment comment = new ShareComment();

        // 对象属性拷贝
        BeanUtils.copyProperties(commentDTO, comment);

        // 判断是否为顶级评论
        if (commentDTO.getParentId() == null){
            // 新增评论为顶级评论
            comment.setParentId(null);
            comment.setTopId(null);
        }else {
            // 新增评论为子评论
            // 判断父评论是否为顶级评论
            ShareComment parentComment = commentMapper.getCommentByCommentId(commentDTO.getParentId());
            if (parentComment.getTopId() != null) {
                // 父评论也是子评论，记录父评论的topId作为当前评论的topId
                comment.setTopId(parentComment.getTopId());
            } else {
                // 父评论是顶级评论，记录父评论的commentId作为当前评论的topId
                comment.setTopId(parentComment.getCommentId());
            }

        }
        // 获取当前登录用户的ID和用户名，作为新增评论数据实体类的属性值
        Map<String, Object> map = ThreadLocalUtil.get();
        Long userId = ((Number) map.get("userId")).longValue();
        String username = (String) map.get("username");

        // 设置新增评论数据实体类的属性值
        comment.setUserId(userId);
        comment.setCreateBy(username);

        // 新增评论数据
        commentMapper.insert(comment);

        // 评论成功后，同步更新分享的评论数量
        shareMapper.increaseCommentCount(comment.getShareId());

        // 查询插入后的评论数据
        ShareComment savedComment = commentMapper.getCommentByCommentId(comment.getCommentId());
    }

    /**
     * 删除评论
     *
     * @param commentId
     */
    @Override
    public void deleteComment(Long commentId) {

        // 获取当前登录用户ID，作为判断是否有权限删除评论的条件
        Map<String, Object> map = ThreadLocalUtil.get();
        Long userId = ((Number) map.get("userId")).longValue();


        // 根据评论ID获取评论数据
        ShareComment comment = commentMapper.selectByCommentId(commentId);

        // 判断当前评论是否属于当前登录用户
        if (! comment.getUserId().equals(userId)){
            throw new NoPermissionToDelete(MessageConstant.NO_PERMISSION_TO_DELETE);
        }

        // 记录删除的评论数量
        int deleteCommentCount = 1;

        // 判断当前是否为顶级评论
        if (comment.getTopId() == null){
            // 记录子评论数量，用于同步更新机会的评论数量
            int totalCommentCount = commentMapper.childByTopId(commentId);
            deleteCommentCount += totalCommentCount;
            // 删除子评论
            commentMapper.deleteCommentByTopId(commentId);
        }
        else {
            // 记录子评论数量，用于同步更新机会的评论数量
            int childCommentCount = commentMapper.childCountByParentId(commentId);
            deleteCommentCount += childCommentCount;
            commentMapper.deleteCommentByParentId(commentId);
        }
        commentMapper.deleteCommentByCommentId(commentId);
        shareMapper.decreaseCommentCount(comment.getShareId(),deleteCommentCount);
    }

    /**
     * 分页查询顶级评论
     *
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult<ShareCommentVO> pageQueryComment(ShareCommentPageQueryDTO pageQueryDTO) {
        // 调用PageHelper分页查询方法，查询评论数据
        PageHelper.startPage(pageQueryDTO.getPageNum(), pageQueryDTO.getPageSize());

        //
        List<ShareCommentVO> commentVOList = commentMapper.pageQueryTopComment(pageQueryDTO.getShareId());

        //
        Page<ShareCommentVO> page = (Page<ShareCommentVO>) commentVOList;

        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 查询回复评论
     *
     * @param commentReplyQueryDTO
     * @return
     */
    @Override
    public List<ShareCommentReplyVO> listQueryReplyComment(ShareCommentReplyQueryDTO commentReplyQueryDTO) {

        // topID作为查询条件，查询回复评论列表
        Long topId = commentReplyQueryDTO.getTopId();
        // parentID作为查询条件，查询回复关系
        Long parentId = commentReplyQueryDTO.getParentId();

        List<ShareCommentReplyVO> replyCommentVOList = commentMapper.listReply(topId, parentId);

        return replyCommentVOList;
    }

    /**
     * 根据用户id查询用户的回复
     *
     * @return
     */
    @Override
    public PageResult< ShareCommentReplyVO> getMyComment(SharePageQueryDto sharePageQueryDto) {
        //获得用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Long userId = ((Number) map.get("userId")).longValue();
        //分页查询
        PageHelper.startPage(sharePageQueryDto.getPageNum(), sharePageQueryDto.getPageSize());
        //查询用户的回复
        List <ShareCommentReplyVO > shareCommentReplyVOS = commentMapper.getMyComment(userId);
        PageResult< ShareCommentReplyVO > pageResult = new PageResult<>();
        //注入查询总数
        pageResult.setTotal(shareCommentReplyVOS.size());
        //注入查询结果
        pageResult.setRecords(shareCommentReplyVOS);
        return pageResult;
    }

    /**
     * 回复我的评论
     *
     * @param sharePageQueryDto
     * @return
     */
    @Override
    public PageResult<ShareCommentReplyVO> CommentReply(SharePageQueryDto sharePageQueryDto) {
        //获得用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Long userId = ((Number) map.get("userId")).longValue();
        PageHelper.startPage(sharePageQueryDto.getPageNum(), sharePageQueryDto.getPageSize());
        //查询回复我的评论
        List<ShareCommentReplyVO> shareCommentReplyVOS = commentMapper.CommentReply(userId);
        PageResult<ShareCommentReplyVO> pageResult = new PageResult<>();
        pageResult.setTotal(shareCommentReplyVOS.size());
        pageResult.setRecords(shareCommentReplyVOS);
        return pageResult;
    }




}
